package com.leon.alishop.spider.spider;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.htmlparser.Parser;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.leon.alishop.core.util.AliShopStringUtils;
import com.leon.alishop.model.GoodsInfo;
import com.leon.alishop.spider.util.AlibabaNodeFilterUtil;
import com.leon.alishop.spider.util.ParserUtil;

public class CrawlAlibabaSpiderImpl implements CrawlAlibabaSpider{
	private static Logger log = LoggerFactory
			.getLogger(CrawlAlibabaSpiderImpl.class);
	
	
	@Override
	public List<GoodsInfo> crawlAllItemList(String homeUrl) {
		String pageUrl = buildAliShopItemListPageUrl(homeUrl);
		
		Parser parser = null;
		
		NodeList nodeList = null;
		LinkTag _node;
		String _url;
		String _name;
		GoodsInfo gi = null;
		List<GoodsInfo> ret = new ArrayList<GoodsInfo>();
		Map<String,String[]> paramsMap;
		do {
			try {
				parser = ParserUtil.newParser(pageUrl);
				if(null == parser){
					log.error("无法访问PageUrl:"
							+ pageUrl);
					break;
				}
			} catch (IOException e) {
				log.error("无法访问PageUrl"+ pageUrl,e);
				break;
			}
			try {
				nodeList = parser.parse(AlibabaNodeFilterUtil.getNextPageFilter(null));
				parser.reset();
			} catch (ParserException e) {
				log.error("无法解析页码:" + pageUrl);
			}
			pageUrl = null;
			if (nodeList != null && nodeList.size() > 0) {
				for (int i = 0, len = nodeList.size(); i < len; i++) {
					_node = (LinkTag) nodeList.elementAt(i);
					_url = _node.getLink();
					if (null == _url) {
						continue;
					}
					pageUrl = _url;
					break;
				}
			}
			// 解析item url
			try {
				nodeList = parser.parse(AlibabaNodeFilterUtil.getItemFilter());
				parser.reset();
			} catch (ParserException e) {
				// crawlFailProviderMap.get(CrawlFailTypeEnum.PARSER_ITEM_ERROR).add(pro.getId());
				log.error("解析商品列表出错"+pageUrl);
			}
			
			if (nodeList == null || nodeList.size() == 0) {
				// crawlFailProviderMap.get(CrawlFailTypeEnum.ITEM_EMPTY).add(pro.getId());
				log.error("商品列表为空！"+pageUrl);
			} else {
				for (int i = 0, len = nodeList.size(); i < len; i++) {
					_node = (LinkTag) nodeList.elementAt(i);
					_url = _node.getLink();
					_name = _node.getAttribute("title"); ;
					if (null == _name) {
						continue;
					}
					_name = AliShopStringUtils.specCToUnicode(_name);
					gi = new GoodsInfo();
					gi.setGoodsName(_name);
					gi.setTaobaoGoodsId(parseTaobaoGoodsIdFromAliItemUrl(_url));
					if(null == gi.getTaobaoGoodsId()){
						log.error("无法解析商品ID url"+_url+"\tname:"+_name);
					}
					gi.setId(parseGoodsIdFromItemName(_name));
					ret.add(gi);
				}
			}
			
			try {
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
				return ret;
			}
		}while(pageUrl != null);
		return ret;
	}
	
	private String buildAliShopItemListPageUrl(String homeUrl){
		if(homeUrl==null || homeUrl.indexOf("http://") != 0){
			return homeUrl;
		}
		if(!homeUrl.endsWith("/")){
			homeUrl = homeUrl + "/";
		}
//		page/offerlist.htm
		return homeUrl + "page/offerlist.htm";
	}
	
	private Long parseTaobaoGoodsIdFromAliItemUrl(String itemUrl){
		int index = 0;
		if((index = itemUrl.indexOf('?'))>0){
			itemUrl = itemUrl.substring(0,index);
		}
		if((index = itemUrl.lastIndexOf('/'))>0){
			itemUrl = itemUrl.substring(index+1,itemUrl.length());
		}
		if(itemUrl.endsWith(".html")){
			itemUrl = itemUrl.substring(0, itemUrl.length()-5);
		}
		try {
			return Long.valueOf(itemUrl);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	private Long parseGoodsIdFromItemName(String itemName){
		int index = 0;
		if((index = itemName.indexOf('#'))>0){
			itemName = itemName.substring(0,index);
		}
		try {
			return Long.valueOf(itemName);
		} catch (Exception e) {
		}
		return null;
	}

	@Override
	public List<GoodsInfo> crawlAllItemList(List<GoodsInfo> giList) {
		
		return null;
	}

}
